home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / Manipulations.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  2.6 KB  |  85 lines

  1. //: C21:Manipulations.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Shows basic manipulations
  7. #include "PrintSequence.h"
  8. #include "NString.h"
  9. #include "Generators.h"
  10. #include <vector>
  11. #include <string>
  12. #include <algorithm>
  13. using namespace std;
  14.  
  15. int main() {
  16.   vector<int> v1(10);
  17.   // Simple counting:
  18.   generate(v1.begin(), v1.end(), SkipGen());
  19.   print(v1, "v1", " ");
  20.   vector<int> v2(v1.size());
  21.   copy_backward(v1.begin(), v1.end(), v2.end());
  22.   print(v2, "copy_backward", " ");
  23.   reverse_copy(v1.begin(), v1.end(), v2.begin());
  24.   print(v2, "reverse_copy", " ");
  25.   reverse(v1.begin(), v1.end());
  26.   print(v1, "reverse", " ");
  27.   int half = v1.size() / 2;
  28.   // Ranges must be exactly the same size:
  29.   swap_ranges(v1.begin(), v1.begin() + half,
  30.     v1.begin() + half);
  31.   print(v1, "swap_ranges", " ");
  32.   // Start with fresh sequence:
  33.   generate(v1.begin(), v1.end(), SkipGen());
  34.   print(v1, "v1", " ");
  35.   int third = v1.size() / 3;
  36.   for(int i = 0; i < 10; i++) {
  37.     rotate(v1.begin(), v1.begin() + third, 
  38.       v1.end());
  39.     print(v1, "rotate", " ");
  40.   }
  41.   cout << "Second rotate example:" << endl;
  42.   char c[] = "aabbccddeeffgghhiijj";
  43.   const char csz = strlen(c);
  44.   for(int i = 0; i < 10; i++) {
  45.     rotate(c, c + 2, c + csz);
  46.     print(c, c + csz, "", "");
  47.   }
  48.   cout << "All n! permutations of abcd:" << endl;
  49.   int nf = 4 * 3 * 2 * 1;
  50.   char p[] = "abcd";
  51.   for(int i = 0; i < nf; i++) {
  52.     next_permutation(p, p + 4);
  53.     print(p, p + 4, "", "");
  54.   }
  55.   cout << "Using prev_permutation:" << endl;
  56.   for(int i = 0; i < nf; i++) {
  57.     prev_permutation(p, p + 4);
  58.     print(p, p + 4, "", "");
  59.   }
  60.   cout << "random_shuffling a word:" << endl;
  61.   string s("hello");
  62.   cout << s << endl;
  63.   for(int i = 0; i < 5; i++) {
  64.     random_shuffle(s.begin(), s.end());
  65.     cout << s << endl;
  66.   }
  67.   NString sa[] = { "a", "b", "c", "d", "a", "b",
  68.     "c", "d", "a", "b", "c", "d", "a", "b", "c"};
  69.   const int sasz = sizeof sa / sizeof *sa;
  70.   vector<NString> ns(sa, sa + sasz);
  71.   print(ns, "ns", " ");
  72.   vector<NString>::iterator it = 
  73.     partition(ns.begin(), ns.end(), 
  74.       bind2nd(greater<NString>(), "b"));
  75.   cout << "Partition point: " << *it << endl;
  76.   print(ns, "", " ");
  77.   // Reload vector:
  78.   copy (sa, sa + sasz, ns.begin());
  79.   it = stable_partition(ns.begin(), ns.end(),
  80.     bind2nd(greater<NString>(), "b"));
  81.   cout << "Stable partition" << endl;
  82.   cout << "Partition point: " << *it << endl;
  83.   print(ns, "", " ");
  84. } ///:~
  85.